#include "getorder.h"
#include "ui_getorder.h"

getorder::getorder(QSqlDatabase* db,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::getorder)
{
    ui->setupUi(this);

    m_db = *db;

    m_menuModel = new QSqlTableModel(this);

    m_menuModel->setTable("tb_menu");

    m_menuModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_menuModel->setHeaderData(1,Qt::Horizontal,"名称",Qt::DisplayRole);
    m_menuModel->setHeaderData(2,Qt::Horizontal,"价格",Qt::DisplayRole);
    m_menuModel->setHeaderData(3,Qt::Horizontal,"类型",Qt::DisplayRole);

    ui->tvMenu->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvMenu->setEditTriggers(QAbstractItemView::NoEditTriggers);

    m_menuModel->select();
    ui->tvMenu->setModel(m_menuModel);

    payModel = new QSqlTableModel(this);
    payModel->setTable("tb_pay");
    payModel->select();

    //初始化付款方式
    for(int i= 0;i < payModel->rowCount();++i){
        if(payModel->data(payModel->index(i,1)).toInt() == 1){

            ui->combPay->addItem(payModel->data(payModel->index(i,2)).toString());
        }
    }

    //初始化可选座位号
    ui->combSeatid->clear();
    m_seatModel = new QSqlTableModel(this);
    m_seatModel->setTable("tb_seat");
    m_seatModel->select();
    for(int i = 0;i < m_seatModel->rowCount();++i){
        if(m_seatModel->data(m_seatModel->index(i,1)).toInt() == 0){
            ui->combSeatid->addItem(m_seatModel->data(m_seatModel->index(i,0)).toString());
        }
    }

    if(ui->combSeatid->count() == 0){
        ui->combSeatid->addItem("无空位置");
    }

}

getorder::~getorder()
{
    delete ui;
}

void getorder::on_btnClear_clicked()
{
    ui->widgetOrder->clear();
}


void getorder::on_btnAppend_clicked()
{

    if(ui->tvMenu->currentIndex().isValid()){
        ui->widgetOrder->addItem(m_menuModel->data(m_menuModel->index(ui->tvMenu->currentIndex().row(),1)).toString()+"*"+ui->spinCount->text());

    }
}


void getorder::on_btnGetOrder_clicked()
{
    double total = 0;
    QString buy;
    int count = ui->widgetOrder->count();
    for(int i= 0;i < count; ++i){

        QStringList alist;
        alist = ui->widgetOrder->item(i)->text().split("*");
        for(int j = 0;j < m_menuModel->rowCount();++j){
            if(m_menuModel->data(m_menuModel->index(j,1)).toString().contains(alist.at(0))){
                total += m_menuModel->data(m_menuModel->index(j,2)).toDouble() * alist.at(1).toInt();
                buy += m_menuModel->data(m_menuModel->index(j,0)).toString()+"*"+alist.at(1)+"&";
                break;
            }
        }
    }

    buy.remove(buy.count()-1,1);

    int result = QMessageBox::information(0,"提示","一共"+QString::asprintf("%.2f",total)+"元,使用"+ui->combPay->currentText()+".",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok);
    if(result == QMessageBox::Ok){
        QSqlQuery query;
        QString seatid = "none";

        seatid = ui->combSeatid->currentText();

        if(seatid == "无空位置"){
            QMessageBox::warning(0,"提示","座位已满,请耐心等待.");
            return;
        }
        if(query.exec("insert into tb_order(buy,total,seatid) values('"+buy+"',"+QString::asprintf("%.2f",total)+","+seatid+");")){

            query.exec("update tb_seat set state=1 where id="+seatid+";");
            QMessageBox::information(0,"提示","下单成功!");

            //初始化可选座位号
            ui->combSeatid->clear();
            m_seatModel->select();
            for(int i = 0;i < m_seatModel->rowCount();++i){
                if(m_seatModel->data(m_seatModel->index(i,1)).toInt() == 0){
                    ui->combSeatid->addItem(m_seatModel->data(m_seatModel->index(i,0)).toString());
                }
            }

            if(ui->combSeatid->count() == 0){
                ui->combSeatid->addItem("无空位置");
            }
        }
    }
    return;
}

